package com.easy.action;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.easy.bean.LayData;
import com.easy.bean.Notice;
import com.easy.service.Service;
import com.easy.service.impl.NoticeService;

@WebServlet("/notice/*")
public class NoticeServlet extends HttpServlet{
	private Service noticeS=new NoticeService();
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uri=req.getRequestURI();
		//uri:  /hotelms/notice/list
		uri=uri.substring(uri.lastIndexOf("/")+1);
		Map params = (Map)req.getAttribute("params");
		System.out.println(uri);
		
		if("list".equals(uri)) {
			List<Notice> list=noticeS.queryList(params);
			int count=noticeS.queryCount(params);
			//封装成LayData对象
			//因为LayUI需要固定的格式来解析数据
			LayData lay=new LayData(count,list);
			//将lay对象转化成json数据,将json数据返回给前台
			String json=JSON.toJSONString(lay);
			resp.getWriter().write(json);
			return;
		}else if("add".equals(uri)) {
			//获取数据,将参数传给service,service交给dao访问数据库
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
			int n_id = Integer.parseInt(req.getParameter("n_id"));
			int u_id = Integer.parseInt(req.getParameter("u_id"));
			try {
				Date n_date = format.parse(req.getParameter("n_date"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int n_readnum = Integer.parseInt(req.getParameter("n_readnum"));
			String n_context = req.getParameter("n_context");
			String n_title = req.getParameter("n_title");
			int count = noticeS.insert(params);
			resp.getWriter().write(count);
			return;
		}else if("del".equals(uri)) {
			
			
			//获取数据,将参数传给service,service交给dao访问数据库
			int n_id = Integer.parseInt(req.getParameter("n_id"));
			int count = noticeS.delete(params);
			resp.getWriter().write(count);
			return;
		}else if("upd".equals(uri)) {
			
			
			
			//获取数据,将参数传给service,service交给dao访问数据库
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			int n_id = Integer.parseInt(req.getParameter("n_id"));
			int u_id = Integer.parseInt(req.getParameter("u_id"));
			try {
				Date n_date = format.parse(req.getParameter("n_date"));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			int n_readnum = Integer.parseInt(req.getParameter("n_readnum"));
			String n_context = req.getParameter("n_context");
			String n_title = req.getParameter("n_title");
			int count = noticeS.update(params);
			resp.getWriter().write(count);
			return;
		}
	}
}
